package pedidos.ejercicio.dao;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import javax.transaction.RollbackException;


import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import pedidos.ejercicio.model.HibernateSessionFactory;

public abstract class BaseDAO {
	Transaction tx;
	Session sess = null;
	public BaseDAO() {
		sess = HibernateSessionFactory.getSession();
	}

	public Serializable save(Object obj) {
		return sess.save(obj);
	}
	
	public Object getObject( Serializable id, Class<?> clase ) {
		return sess.get(clase, id);
	}
	
	public void delete(Object obj) {
		
		sess.delete(obj);
	}
	
	public void initTransaction() {
		tx = sess.beginTransaction();
	}
	public void commit(){
		if ( tx != null ) {
			tx.commit();
		}
	}
	
	public void rollback(){
		if ( tx != null ) {
			tx.rollback();
		}
	}
	
	public void closeSession() {
		sess.close();
	}

	@SuppressWarnings("unchecked")
	public List<?> executeNamedQuery( String queryName, 
											HashMap<String, Object> params ) {
		List<Object> lst = new ArrayList<Object>(0);
		Query query = sess.getNamedQuery(queryName);
		if ( params != null ) {
			Set<String> keys = params.keySet();
			for ( String key : keys ) {
				Object myParam = params.get(key);
				query.setParameter(key, myParam);
			}
		}
		lst = (List<Object>) query.list();
		return lst;
	}
}
